from fastapi import APIRouter, Depends, Request
from module_admin.service.login_service import LoginService
from module_admin.entity.vo.employee_salary_vo import EmployeeSalaryModel, EmployeeSalaryQueryModel
from typing import List
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from sqlalchemy.ext.asyncio import AsyncSession
from config.get_db import get_db
from module_admin.aspect.data_scope import GetDataScope
from module_admin.service.employee_salary_service import EmployeeSalaryService
from utils.log_util import logger
from utils.response_util import ResponseUtil
from pydantic_validation_decorator import ValidateFields
from module_admin.annotation.log_annotation import Log
from config.enums import BusinessType
from datetime import datetime

# 员工薪资管理 -路由-实现
employeeSalaryController = APIRouter(prefix='/system/employee-salary', dependencies=[Depends(LoginService.get_current_user)])

# 功能1：获取员工薪资列表
@employeeSalaryController.get( '/list', response_model=List[EmployeeSalaryModel], dependencies=[Depends(CheckUserInterfaceAuth('system:employee_salary:list'))])
async def get_system_employee_salary_list(
    request: Request,
    salary_query: EmployeeSalaryQueryModel = Depends(EmployeeSalaryQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('EmployeeSalary')),
):
    logger.info('开始获取员工薪资列表')
    salary_query_result = await EmployeeSalaryService.get_employee_salary_list_services(query_db, salary_query, data_scope_sql, is_page=True)
    logger.info('获取员工薪资列表成功')
    return ResponseUtil.success(model_content=salary_query_result)

# 功能2：获取单个员工薪资详情
@employeeSalaryController.get(
    '/{salary_id}', response_model=EmployeeSalaryModel, dependencies=[Depends(CheckUserInterfaceAuth('system:employee_salary:query'))]
)
async def get_system_employee_salary(
    salary_id: int,
    query_db: AsyncSession = Depends(get_db)
):
    logger.info(f'开始获取员工薪资详情，ID: {salary_id}')
    result = await EmployeeSalaryService.get_employee_salary_services(query_db, salary_id)
    logger.info(f'获取员工薪资详情成功，ID: {salary_id}')
    return ResponseUtil.success(data=result)

# 功能3：新增员工薪资
@employeeSalaryController.post('', dependencies=[Depends(CheckUserInterfaceAuth('system:employee_salary:add'))])
@ValidateFields(validate_model='employee_salary')
@Log(title='员工薪资管理', business_type=BusinessType.INSERT)
async def add_system_employee_salary(
    request: Request,
    salary: EmployeeSalaryModel,
    query_db: AsyncSession = Depends(get_db),
    current_user=Depends(LoginService.get_current_user)
):
    logger.info('开始新增员工薪资')
    # 自动填充创建人和时间
    salary.create_by = current_user.user.user_name
    salary.create_time = datetime.now()
    salary.update_by = current_user.user.user_name
    salary.update_time = datetime.now()
    # 业务逻辑
    result = await EmployeeSalaryService.add_employee_salary_services(query_db, salary)
    logger.info('新增员工薪资成功')
    return ResponseUtil.success(msg='新增成功', data=result)

# 功能4：修改员工薪资
@employeeSalaryController.put('', dependencies=[Depends(CheckUserInterfaceAuth('system:employee_salary:edit'))])
@ValidateFields(validate_model='employee_salary')
@Log(title='员工薪资管理', business_type=BusinessType.UPDATE)
async def update_system_employee_salary(
    request: Request,
    salary: EmployeeSalaryModel,
    query_db: AsyncSession = Depends(get_db),
    current_user=Depends(LoginService.get_current_user)
):
    logger.info(f'开始修改员工薪资，ID: {salary.id}')
    # 自动填充更新人和时间
    salary.update_by = current_user.user.user_name
    salary.update_time = datetime.now()
    # 业务逻辑
    result = await EmployeeSalaryService.update_employee_salary_services(query_db, salary)
    logger.info(f'修改员工薪资成功，ID: {salary.id}')
    return ResponseUtil.success(msg='修改成功', data=result)

# 功能5：删除员工薪资
@employeeSalaryController.delete('/{salary_id}', dependencies=[Depends(CheckUserInterfaceAuth('system:employee_salary:remove'))])
@Log(title='员工薪资管理', business_type=BusinessType.DELETE)
async def delete_system_employee_salary(
    salary_id: int,
    query_db: AsyncSession = Depends(get_db),
    current_user=Depends(LoginService.get_current_user)
):
    logger.info(f'开始删除员工薪资，ID: {salary_id}')
    result = await EmployeeSalaryService.delete_employee_salary_services(query_db, salary_id, current_user.user.user_name)
    logger.info(f'删除员工薪资成功，ID: {salary_id}')
    return ResponseUtil.success(msg='删除成功', data=result)

# 功能6：导出员工薪资
@employeeSalaryController.get('/export', dependencies=[Depends(CheckUserInterfaceAuth('system:employee_salary:export'))])
@Log(title='员工薪资管理', business_type=BusinessType.EXPORT)
async def export_system_employee_salary(
    request: Request,
    salary_query: EmployeeSalaryQueryModel = Depends(EmployeeSalaryQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('EmployeeSalary')),
):
    logger.info('开始导出员工薪资数据')
    result = await EmployeeSalaryService.export_employee_salary_services(query_db, salary_query, data_scope_sql)
    logger.info('导出员工薪资数据成功')
    return ResponseUtil.success(data=result)
